package com.hs.web;

import com.hs.pojo.Dept;
import com.hs.pojo.Teacher;
import com.hs.service.DeptService;
import com.hs.service.TeacherService;
import com.hs.service.impl.DeptServiceImpl;
import com.hs.service.impl.TeacherServiceImpl;
import com.hs.util.Page;
import com.hs.util.UploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/teacher/teacherServlet")
public class TeacherServlet extends HttpServlet {

    TeacherService teacherService=new TeacherServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的action
        String action=req.getParameter("action");
        if("selectTeachers".equals(action)){
            String pageNum = req.getParameter("pageNum");
            if (pageNum == null){
                pageNum ="1";
            }
            int pn=Integer.parseInt(pageNum);
            Page<Teacher> page = teacherService.selectTeachers(pn);
            //封装page对象 同时跳转页面
            req.setAttribute("page",page);
            req.getRequestDispatcher("/pages/teacher-list.jsp").forward(req,resp);
        }else if ("insertTeacher".equals(action)){
            Map<String,String> map= UploadUtil.upload(req);
            String name = map.get("name");
            String img = map.get("img");
            String phone = map.get("phone");
            String job= map.get("job");
            String style=map.get("style");
            String detail=map.get("detail");
            Integer did = Integer.valueOf(map.get("did"));
            Teacher teacher=new Teacher(0,name,img,phone,job,style,detail,did);
            int insert = teacherService.insertTeacher(teacher);
            String mess="";
            if(insert!=0){
                mess="添加成功";
            }else{
                mess="添加失败";
            }
            req.setAttribute("mess",mess);
            req.getRequestDispatcher("/teacher/teacherServlet?action=selectTeachers").forward(req,resp);
        }else if ("selectTeacherById".equals(action)){
            String id=req.getParameter("id");
            Teacher teacher = teacherService.selectTeacherById(Integer.parseInt(id));
            req.setAttribute("teacher",teacher);
            //部门选择
            DeptService deptService=new DeptServiceImpl();
            List<Dept> depts = deptService.selectAll();
            req.setAttribute("depts",depts);

            req.getRequestDispatcher("/pages/teacher-update.jsp").forward(req,resp);

        }else if("updateTeacher".equals(action)){
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String img = req.getParameter("img");
            String phone = req.getParameter("phone");
            String job = req.getParameter("job");
            String style = req.getParameter("style");
            String detail = req.getParameter("detail");
            String did=req.getParameter("did");
            Teacher teacher=new Teacher(Integer.parseInt(id),name,img,phone,job,style,detail,Integer.parseInt(id));
            int update = teacherService.updateTeacher(teacher);
            String mess="";
            if(update!=0){
                mess="修改成功";
            }else{
                mess="修改失败";
            }
            req.setAttribute("mess",mess);
            req.getRequestDispatcher("/teacher/teacherServlet?action=selectTeachers").forward(req,resp);
        }else if("deleteTeacher".equals(action)){
            String id = req.getParameter("id");
            int delete = teacherService.deleteTeacher(Integer.parseInt(id));
            String mess="";
            if(delete!=0){
                mess="删除成功";
            }else{
                mess="删除失败";
            }
            req.setAttribute("mess",mess);
            req.getRequestDispatcher("/teacher/teacherServlet?action=selectTeachers").forward(req,resp);
        }else if ("selectByIdForShow".equals(action)){  //根据编号查询
            String id = req.getParameter("id");
            Teacher teacher = teacherService.selectTeacherById(Integer.parseInt(id));
            req.setAttribute("teacher",teacher);
            req.getRequestDispatcher("/pages/teacher-detail.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}
